package com.dms.common.api;

import com.dms.common.exception.BusinessException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
@ApiModel(description = "API统一响应结果")
public class ApiResponse<T> {

    @ApiModelProperty(value = "状态码", required = true)
    private Integer code;

    @ApiModelProperty(value = "提示信息", required = true)
    private String message;

    @ApiModelProperty(value = "数据")
    private T data;

    // 成功响应
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(ApiResponseCode.SUCCESS, "success", data);
    }

    // 成功响应（无数据）
    public static <T> ApiResponse<T> success() {
        return new ApiResponse<>(ApiResponseCode.SUCCESS, "success", null);
    }

    // 错误响应
    public static <T> ApiResponse<T> error(String message) {
        log.error("Error: {}", message);
        return new ApiResponse<>(ApiResponseCode.SYSTEM_ERROR, message, null);
    }

    // 自定义错误响应
    public static <T> ApiResponse<T> error(Integer code, String message) {
        log.error("Error: code={}, message={}", code, message);
        return new ApiResponse<>(code, message, null);
    }

    // 业务异常响应
    public static <T> ApiResponse<T> error(BusinessException e) {
        log.error("Business error: {}", e.getMessage());
        return new ApiResponse<>(e.getCode(), e.getMessage(), null);
    }

    // 系统异常响应
    public static <T> ApiResponse<T> error(Exception e) {
        log.error("System error: ", e);
        return new ApiResponse<>(ApiResponseCode.SYSTEM_ERROR, "系统错误", null);
    }

    // 参数错误响应
    public static <T> ApiResponse<T> invalidParam(String message) {
        log.warn("Invalid parameter: {}", message);
        return new ApiResponse<>(ApiResponseCode.INVALID_PARAMETER, message, null);
    }

    // 验证失败响应
    public static <T> ApiResponse<T> validateFailed(String message) {
        log.warn("Validation failed: {}", message);
        return new ApiResponse<>(ApiResponseCode.BAD_REQUEST, message, null);
    }

    // 未授权响应
    public static <T> ApiResponse<T> unauthorized(String message) {
        log.warn("Unauthorized: {}", message);
        return new ApiResponse<>(ApiResponseCode.UNAUTHORIZED, message, null);
    }

    // 禁止访问响应
    public static <T> ApiResponse<T> forbidden(String message) {
        log.warn("Forbidden: {}", message);
        return new ApiResponse<>(ApiResponseCode.FORBIDDEN, message, null);
    }

    // 资源不存在响应
    public static <T> ApiResponse<T> notFound(String message) {
        log.warn("Not found: {}", message);
        return new ApiResponse<>(ApiResponseCode.NOT_FOUND, message, null);
    }

    // 业务逻辑错误
    public static <T> ApiResponse<T> businessFailed(String message) {
        log.error("Business failed: {}", message);
        return new ApiResponse<>(ApiResponseCode.BUSINESS_FAILED, message, null);
    }

    // 服务不可用错误
    public static <T> ApiResponse<T> serviceUnavailable(String message) {
        log.error("Service unavailable: {}", message);
        return new ApiResponse<>(503, message, null);
    }

    // 分页响应
    public static <T> ApiResponse<PageResult<T>> page(T data, long total) {
        PageResult<T> pageResult = new PageResult<>(data, total);
        return success(pageResult);
    }
} 